﻿/*
 * Created by SharpDevelop.
 * User: mac
 * Date: 26.01.2007
 * Time: 19:42
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using TagTool.Core;
using System.IO;
using System.Collections.Generic;
using TagTool.GraphicTypes;

namespace TagTool.Nodes.Misc
{

	public class Directory : BaseNode
	{
		private OutputSlot file;
		private OutputSlot count;
		private InputSlot selected;

		private List<String> files = new List<String>();
		
		public Directory() : base()
		{
			file = new OutputSlot(this, "File", typeof(String));
			file.Value = "";
			count = new OutputSlot(this, "Count", typeof(double));
			count.Value = 0.0;
			selected = new InputSlot(this, "Selected", -1.0);
			selected.ParseCallback = ParseHelper.ParseDouble;
			Name = "Directory";
			Initialize();
		}
		
		public override void Initialize()
		{
			base.Initialize();
			selected.Value = -1;
			file.Value = "";
			ReadDir();
			Update(TriggerIn);
		}
		
		private void ReadDir()
		{
			files = new List<String>();
			String cut;
			try {
				files.Clear();
				String path = ProjectManager.Instance.AppDir + "\\sessions\\" + ProjectManager.Instance.SessionName;
				String[] fi = System.IO.Directory.GetFiles(path);
				Console.WriteLine(path);
				foreach(String s in fi) 
				{
					if (s.EndsWith(".tag"))
					{
						cut = s.Remove(0, path.Length + 1);
						Console.WriteLine(s);
						Console.WriteLine(cut);
						files.Add(cut);
						
					}
				}
			} 
			catch (Exception ex)
			{
				Console.WriteLine(ex.Message);
			}

		}
		
		public override void Update(Object sender)
		{
			if ((bool)this.TriggerIn.Value)
			{
				ReadDir();
			}
			
			if ((double)selected.Value >= 0.0 && files.Count > 0 && (double)selected.Value <= files.Count - 1)
			{
				file.Value = files[(int)(double)selected.Value];
			}
			count.Value = (double)files.Count;
			
			CastTrigger();
		}
		
		[NodeAttribute(SlotType.Output, Show = true)]
		public OutputSlot File
		{
			get { return file; }
		}
		
		[NodeAttribute(SlotType.Output, Show = true)]
		public OutputSlot Count
		{
			get { return count; }
		}
		
		[NodeAttribute(SlotType.Input, Show = true)]
		public InputSlot Selected
		{
			get { return selected; }
		}
		
		
	}
}
